package com.example.win.android_banduyujia.peopleactivity.helper;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

import com.example.win.android_banduyujia.entity.ImageBucket;
import com.example.win.android_banduyujia.entity.ImageItem;
import android.provider.MediaStore.Images.Media;
import android.provider.MediaStore.Images.Thumbnails;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 专辑帮助类
 * Created by Win on 2015/9/10.
 */
public class AlbumHelper {
    final String TAG = getClass().getSimpleName();

    public Context  context;
    public ContentResolver  cr;

    // 缩略图列表
    HashMap<String, String> thumbnailList = new HashMap<String, String>();
    // 专辑列表
    List<HashMap<String, String>>  albumList=new ArrayList<HashMap<String, String>>();
    HashMap<String, ImageBucket>   bucketList=new HashMap<String, ImageBucket>();

    private static AlbumHelper instance;

    private AlbumHelper(){

    }
    public static AlbumHelper getHelper() {
        if(instance == null){
            instance = new AlbumHelper();
        }
        return instance;
    }

    public void init(Context Context) {
         if(this.context == null){
             this.context = Context;
             cr = context.getContentResolver();
         }
    }

    /**
     * 得到缩略图
     */
    public void getThumbnail() {
            String[]  projection={Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA};
            Cursor   cursor = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
            getThumbnailColumnData(cursor);
    }

    /**
     * 从数据库中得到缩略图
     * @param cur
     */
    private void getThumbnailColumnData(Cursor cur) {
         if(cur.moveToFirst()){
             int     _id;
             int     image_id;
             String  image_path;
             int  _idColumn = cur.getColumnIndex(Thumbnails._ID);
             int  image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);
             int  dataColumn = cur.getColumnIndex(Thumbnails.DATA);

             do{
                 _id = cur.getInt(_idColumn);
                 image_id=cur.getInt(image_idColumn);
                 image_path=cur.getString(dataColumn);

                 thumbnailList.put(" " + image_id, image_path);
             }while (cur.moveToNext());
         }
    }

    /**
     * 是否创建了图片集
     */
    boolean hasBuildImagesBucketList = false;

    /**
     * 得到图片集
     *
     * @param refresh
     * @return
     */
    public List<ImageBucket> getImagesBucketList(boolean refresh) {
         if(refresh || (!refresh && !hasBuildImagesBucketList)){
                buildImagesBucketList();
            }

         List<ImageBucket>  tmpList=new ArrayList<ImageBucket>();
         Iterator<Map.Entry<String, ImageBucket>>  itr=bucketList.entrySet().iterator();
        while(itr.hasNext()){
            Map.Entry<String, ImageBucket> entry = itr.next();
            tmpList.add(entry.getValue());
        }
        return tmpList;
    }

    /**
     * 得到图片集
     */
    private void buildImagesBucketList() {
          long startTime =System.currentTimeMillis();

            // 构造缩略图索引
            getThumbnail();

        // 构造相册索引
        String columns[] = new String[]{Media._ID, Media.BUCKET_ID, Media.PICASA_ID,
                Media.DATA, Media.DISPLAY_NAME, Media.TITLE,
                Media.SIZE, Media.BUCKET_DISPLAY_NAME};

        // 得到一个游标
        Cursor  cur = cr.query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);

        if (cur.moveToFirst()) {
            // 获取指定列的索引
            int photoIDIndex = cur.getColumnIndexOrThrow(Media._ID);
            int photoPathIndex = cur.getColumnIndexOrThrow(Media.DATA);
            int photoNameIndex = cur.getColumnIndexOrThrow(Media.DISPLAY_NAME);
            int photoTitleIndex = cur.getColumnIndexOrThrow(Media.TITLE);
            int photoSizeIndex = cur.getColumnIndexOrThrow(Media.SIZE);
            int bucketDisplayNameIndex = cur
                    .getColumnIndexOrThrow(Media.BUCKET_DISPLAY_NAME);
            int bucketIdIndex = cur.getColumnIndexOrThrow(Media.BUCKET_ID);
            int picasaIdIndex = cur.getColumnIndexOrThrow(Media.PICASA_ID);
            // 获取图片总数
            int totalNum = cur.getCount();

            do {
                String _id = cur.getString(photoIDIndex);
                String name = cur.getString(photoNameIndex);
                String path = cur.getString(photoPathIndex);
                String title = cur.getString(photoTitleIndex);
                String size = cur.getString(photoSizeIndex);
                String bucketName = cur.getString(bucketDisplayNameIndex);
                String bucketId = cur.getString(bucketIdIndex);
                String picasaId = cur.getString(picasaIdIndex);

                Log.i(TAG, _id + ", bucketId: " + bucketId + ", picasaId: "
                        + picasaId + " name:" + name + " path:" + path
                        + " title: " + title + " size: " + size + " bucket: "
                        + bucketName + "---");

                ImageBucket bucket = bucketList.get(bucketId);
                if (bucket == null)
                {
                    bucket = new ImageBucket();
                    bucketList.put(bucketId, bucket);
                    bucket.imageList = new ArrayList<ImageItem>();
                    bucket.bucketName = bucketName;
                }
                bucket.count++;
                ImageItem imageItem = new ImageItem();
                imageItem.imageId = _id;
                imageItem.imagePath = path;
                imageItem.thumbnailPath = thumbnailList.get(_id);
                bucket.imageList.add(imageItem);

            } while (cur.moveToNext());
        }

        Iterator<Map.Entry<String, ImageBucket>> itr = bucketList.entrySet().iterator();
        while (itr.hasNext())
        {
            Map.Entry<String, ImageBucket> entry = itr.next();
            ImageBucket bucket = entry.getValue();
            Log.d(TAG, entry.getKey() + ", " + bucket.bucketName + ", " + bucket.count + " ---------- ");
            for (int i = 0; i < bucket.imageList.size(); i++) {
                ImageItem image = bucket.imageList.get(i);
                Log.d(TAG, "----- " + image.imageId + ", " + image.imagePath + ", " + image.thumbnailPath);
            }
        }
        hasBuildImagesBucketList = true;
        long endTime = System.currentTimeMillis();
        Log.d(TAG, "use time: " + (endTime - startTime) + " ms");
    }
}
